package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Announce;
import cn.sunlelin.educationserver.mapper.AnnounceMapper;
import cn.sunlelin.educationserver.service.AnnounceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AnnounceServiceImpl implements AnnounceService {
    @Autowired
    private AnnounceMapper announceMapper;

    @Override
    public Boolean createAnnounce(Announce announce) {
        announce.setCreateTime(new Date());
        announceMapper.createAnnounce(announce);
        return true;
    }

    @Override
    public Map<String, Object> selectAnnounceById(Integer announceId) {
        return announceMapper.selectAnnounceById(announceId);
    }

    @Override
    public Page<Map<String, Object>> getPageByCourseId(Integer currentPage, Integer pageSize, Integer courseId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> announceList=announceMapper.selectAnnounceByCourseId(courseId);
        return(Page<Map<String, Object>>) announceList;
    }

    @Override
    public Boolean updateAnnounce(Announce announce) {
        if(announceMapper.selectAnnounceById(announce.getAnnounceId())==null)
            return false;
        announce.setUpdateTime(new Date());
        announceMapper.updateAnnounce(announce);
        return true;
    }

    @Override
    public Boolean deleteAnnounce(Integer announceId) {
        if(announceMapper.selectAnnounceById(announceId)==null)
            return false;
        Announce announce=new Announce();
        announce.setAnnounceId(announceId);
        announce.setUpdateTime(new Date());
        announceMapper.deleteAnnounce(announce);
        return true;
    }
}
